/*
 * Copyright (C) 2009 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.bookmark;

import static cc.aileron.workflow.WorkflowMethod.*;
import cc.aileron.bookmark.process.ConfirmProcess;
import cc.aileron.bookmark.process.ListProcess;
import cc.aileron.bookmark.process.RegistryProcess;
import cc.aileron.bookmark.resource.EntryResource;
import cc.aileron.bookmark.resource.ListResource;
import cc.aileron.commons.resource.ResourceNotFoundException;
import cc.aileron.template.parser.ParserMethodNotFoundException;
import cc.aileron.template.reader.TemplateSyntaxEexception;
import cc.aileron.workflow.WorkflowBinder;
import cc.aileron.workflow.WorkflowConfigure;
import cc.aileron.workflow.WorkflowProcess;
import cc.aileron.workflow.WorkflowTransition;
import cc.aileron.wsgi.extension.template.TemplateProcessProvider;

import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
 * @author Aileron
 */
@Singleton
public class ApplicationConfigure implements WorkflowConfigure
{
    @Override
    public void configure(final WorkflowBinder binder) throws Exception
    {
        /**
         * list
         */
        binder.bind()
            .type(ListResource.class)
            .method(GET)
            .uri("/")
            .to()
            .requestParameterKeys("paging.page")
            .process(ListProcess.class)
            .process(list);

        /**
         * entry-form
         */
        binder.bind()
            .type(EntryResource.class)
            .method(GET)
            .uri("/entry")
            .to()
            .process(entry);

        /**
         * entry-confirm
         */
        binder.bind()
            .type(EntryResource.class)
            .method(POST)
            .uri("/entry")
            .overrideKey("confirm")
            .to()
            .requestParameterKeys("bookmark.title")
            .requestParameterKeys("bookmark.string")
            .process(ConfirmProcess.class)
            .process(confirm);

        /**
         * entry-complete
         */
        binder.bind()
            .type(EntryResource.class)
            .method(POST)
            .uri("/entry")
            .to()
            .process(RegistryProcess.class)
            .process(WorkflowTransition.LOCALREDIRECT, "/");
    }

    /**
     * @param p
     * @throws ResourceNotFoundException
     * @throws ParserMethodNotFoundException
     * @throws TemplateSyntaxEexception
     */
    @Inject
    public ApplicationConfigure(final TemplateProcessProvider p) throws TemplateSyntaxEexception,
            ParserMethodNotFoundException,
            ResourceNotFoundException
    {
        list = p.get("list.html");
        entry = p.get("entry.html");
        confirm = p.get("confirm.html");
    }

    private final WorkflowProcess<Object> confirm;
    private final WorkflowProcess<Object> list;
    private final WorkflowProcess<Object> entry;
}
